<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace app\admin\controller;

use api\mall\model\NotificationModel;
use cmf\controller\AdminBaseController;
use think\db\Query;

/**
 * Description of NotificationController
 *
 * @author PC
 */
class NotificationController extends AdminBaseController
{

    /**
     * 通知管理
     * @adminMenu(
     *     'name'   => '通知管理',
     *     'parent' => 'admin/Notification/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '通知管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();

        $notificationModel = new NotificationModel();
        $notificationsQuery = $notificationModel->alias('a');
        $notifications = $notificationsQuery
            ->where(function (Query $query) use ($param) {
                $keyword = empty($param['keyword']) ? '' : $param['keyword'];
                if (!empty($keyword)) {
                    $query->where('title', 'like', "%$keyword%");
                }
            })
            ->order('notification_type', 'DESC')
            ->order('sort_order', 'DESC')
            ->paginate(20);

        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('pages', $notifications->items());
        $this->assign('page', $notifications->render());
        return $this->fetch('index');
    }

    /**
     * 添加通知
     * @adminMenu(
     *     'name'   => '添加通知',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加通知',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加通知提交
     * @adminMenu(
     *     'name'   => '添加通知提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加通知提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data = $this->request->param();

        $data['create_time'] = time();
        $data['update_time'] = time();

        $notificationModel = new NotificationModel();
        $notificationModel->adminAddNotification($data);
        $this->success(lang("SAVE_SUCCESS"));
    }

    // public function add()
    // {
    //     $type = $this->request->route('type/d', 1);
    //     $this->assign('type', $type);
    //     $storeModel = new StoreModel();
    //     $items = $storeModel->types();
    //     $this->assign('item_types', $items);
    //     return $this->fetch();
    // }

    /**
     * 编辑通知
     * @adminMenu(
     *     'name'   => '编辑通知',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑通知',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $notificationModel = new NotificationModel();
        $data = $notificationModel->where('id', $id)->find();
        $this->assign('data', $data);

        return $this->fetch();
    }

    /**
     * 编辑通知提交
     * @adminMenu(
     *     'name'   => '编辑通知提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑通知提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();

        $data['update_time'] = time();

        $notificationModel = new NotificationModel();

        $notification = NotificationModel::find($data['id']);
        $notification->save($data);

        // $notificationModel->where('id', $data['id'])->update($data);
        $this->success(lang("SAVE_SUCCESS"));
    }

    /**
     * 通知删除
     * @adminMenu(
     *     'name'   => '通知删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '通知删除',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function delete()
    {
        $param = $this->request->param();
        if (isset($param['id'])) {
            $notificationModel = new NotificationModel();
            $notificationModel->where('id', $param['id'])->delete();
            $this->success("删除成功！", '');

        } else {
            $this->error("删除失败！");
        }
    }
}
